package org.iocn.core.tags;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
import org.iocn.common.Page;

/**
 * JSP页面分页标签实现类<br>
 * 
 * 依赖jar：<br>
 *      org.apache.taglibs.standard.jar
 * @author Oxidy
 * @version 0.1,20100603
 *
 */
public class PageTag extends BaseTagSupport {
    private static final long serialVersionUID = -1698292190359898746L;
    
    //private Object pageURL;
    private Object pageCSS;
    private Object formID;
    private Object jQueryURL;
    private int multiPage = 0;
    
    public int doStartTag() throws JspException{
        try {
            HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
            Object obj = request.getAttribute("page");
            if (obj == null) {
                return EVAL_PAGE;
            }
            if (!(obj instanceof Page)) {
                return EVAL_PAGE;
            }
            Page pageInfo = (Page)obj;
            //Page pages = (Page)request.getAttribute("page");
            StringBuffer stringBuffer = new StringBuffer();
            if (pageCSS == null) {
                pageCSS = "";
            }
            stringBuffer.append("<div class=\"").append(pageCSS.toString()).append("\">");
            if (multiPage == 0) {
                //===============添加JQuery地址===============
                if (jQueryURL != null) {
                    stringBuffer.append("<script src=\"").append(jQueryURL.toString())
                        .append("\" type=\"text/javascript\"></script>");
                }
                //===============拼接隐藏域中的参数===============
                stringBuffer.append("<input type=\"hidden\" name=\"page.pageNo\" id=\"pageNo\" value=\"")
                    .append(pageInfo.getPageNo()).append("\"/>")
                    .append("<input type=\"hidden\" name=\"page.orderBy\" id=\"orderBy\" value=\"")
                    .append(isNullToEmpty(pageInfo.getOrderBy())).append("\"/>")
                    .append("<input type=\"hidden\" name=\"page.order\" id=\"order\" value=\"")
                    .append(pageInfo.getOrder()).append("\"/>");
            }
            //==============拼接显示内容===============
            stringBuffer.append("共").append(pageInfo.getTotalCount()).append("条记录&nbsp;&nbsp;")
                .append("每页").append(pageInfo.getPageSize()).append("条记录&nbsp;&nbsp;")
                .append("当前第").append(pageInfo.getPageNo()).append("/").append(pageInfo.getTotalPages()).append("页")
                .append("&nbsp;&nbsp;")
                .append("<a href=\"javascript:jumpPage(1)\">首页</a>").append("&nbsp;");
            if (pageInfo.isHasPre()) {
                stringBuffer.append("<a href=\"javascript:jumpPage(").append(pageInfo.getPrePage()).append(")\">上一页</a>").append("&nbsp;");
            }else {
                stringBuffer.append("上一页").append("&nbsp;");
            }
            if (pageInfo.isHasNext()) {
                stringBuffer.append("<a href=\"javascript:jumpPage(").append(pageInfo.getNextPage()).append(")\">下一页</a>").append("&nbsp;");
            }else {
                stringBuffer.append("下一页").append("&nbsp;");
            }
                stringBuffer.append("<a href=\"javascript:jumpPage(").append(pageInfo.getTotalPages()).append(")\">末页</a>").append("&nbsp;")
                .append("跳转到____页");
                //==========拼装Query跳转方法JavaScript脚本===============
            if (multiPage == 0) {
                stringBuffer.append("<script type=\"text/javascript\">").append("function jumpPage(pageNo) {")
                    .append("\n $(\"#pageNo\").val(pageNo);\n");
//                if (pageURL != null) {
//                    stringBuffer.append("$(\"#").append(formID).append("\").action=\"").append(pageURL).append("\"");
//                }
                stringBuffer.append("$(\"#").append(formID).append("\").submit();}")
                    .append("</script>");
            }
            stringBuffer.append("</div>"); 
            if (stringBuffer !=null) {
                JspWriter out = pageContext.getOut();
                out.print(stringBuffer.toString());
            }
        } catch (Exception e) {
            throw new JspException(e);
        }
        return EVAL_PAGE;
        
    }
    //==========================================
//    /**
//     * 页面跳转URL路径信息，支持EL表达式的写法
//     */
//    public void setPageURL(Object pageURL) throws JspException {
//        this.pageURL = ExpressionEvaluatorManager.evaluate("pageURL", pageURL.toString(), Object.class, this, pageContext);
//    }
    /**
     * 分页样式，支持EL表达式的写法
     * @param cssClass
     * @throws JspException
     */
    public void setPageCSS(Object pageCSS) throws JspException {
        this.pageCSS = ExpressionEvaluatorManager.evaluate("pageCSS", pageCSS.toString(), Object.class, this, pageContext);
    }
    /**
     * Form表单ID，支持EL表达式的写法
     * @param formID
     * @throws JspException
     */
    public void setFormID(Object formID) throws JspException {
        this.formID = ExpressionEvaluatorManager.evaluate("formID", formID.toString(), Object.class, this, pageContext);
    }
    /**
     * JQuery脚本库地址，支持EL表达式的写法
     * @param jQueryURL
     * @throws JspException
     */
    public void setJQueryURL(Object jQueryURL) throws JspException {
        this.jQueryURL = ExpressionEvaluatorManager.evaluate("jQueryURL", jQueryURL.toString(), Object.class, this, pageContext);
    }
    public void setMultiPage(int multiPage){
        this.multiPage = multiPage;
    }
}
